/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.DismantlePileStackDao;
import com.sigmatrix.h5.dao.impl.common.impl.BaseDaoImpl;
import com.sigmatrix.h5.entity.DismantlePileStack;
import com.sigmatrix.h5.enums.TableEnum;
import com.sigmatrix.h5.exception.DaoException;

/**
 * @ClassName: DismantlePileStackDaoImpl
 * @Description: 拆组垛数据库操作接口实现
 * @author youjun 
 * @date 2017-08-17 20:37:22
 */
@Repository(value = "dismantlePileStackDao")
public class DismantlePileStackDaoImpl extends BaseDaoImpl<DismantlePileStack> implements DismantlePileStackDao {

	/** 注入jdbcTemplate */
	@Autowired
	private transient JdbcTemplate jdbcTemplate;

	@Override
	public DismantlePileStack findById(final Long id, final String enterpriseCode) throws DaoException {
		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(getTname(enterpriseCode, TableEnum.DISMANTLE_PILE_STACK)).append(" WHERE _id = ?");
		final Object[] params = new Object[] { id };
		final List<DismantlePileStack> list = jdbcTemplate.query(sql.toString(), params, new DismantlePileStackRowMapper());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public int save(final DismantlePileStack dismantlePileStack, final String enterpriseCode) throws DaoException {

		final StringBuilder sql = new StringBuilder("INSERT INTO ")
				.append(getTname(enterpriseCode, TableEnum.DISMANTLE_PILE_STACK))
				.append("(_work_order, _number, _create_time, _creator_id, _terminal_id, _warehouse_id, _product_id,")
				.append(" _current_wrap_code, _sub_wrap_codes, _opre_type, _remarks) ")
				.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
		final Object[] params = new Object[] { dismantlePileStack.getWorkOrder(), dismantlePileStack.getNumber(),
				dismantlePileStack.getCreateTime(), dismantlePileStack.getCreatorId(),
				dismantlePileStack.getTerminalId(), dismantlePileStack.getWarehouseId(),
				dismantlePileStack.getProductId(), dismantlePileStack.getCurrentWrapCode(),
				dismantlePileStack.getSubWrapCodes(), dismantlePileStack.getOpreType(), 
				dismantlePileStack.getRemarks()
				};
		final int[] argTypes = new int[] { Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.INTEGER, Types.INTEGER,
				Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.LONGVARCHAR, Types.SMALLINT, Types.VARCHAR,
				};
		return jdbcTemplate.update(sql.toString(), params, argTypes);
	}

	@Override
	public int update(final DismantlePileStack dismantlePileStack, final String enterpriseCode) throws DaoException {

		final StringBuilder sql = new StringBuilder("UPDATE ")
				.append(getTname(enterpriseCode, TableEnum.DISMANTLE_PILE_STACK))
				.append(" SET _work_order = ?, _number = ?, _create_time = ?, _creator_id = ?, ")
				.append( "_terminal_id = ?, _warehouse_id = ?, _product_id = ?, _current_wrap_code = ?, ")
				.append("_sub_wrap_codes = ?, _opre_type = ?, _remarks = ? WHERE _id = ?");
		final Object[] params = new Object[] { dismantlePileStack.getWorkOrder(), dismantlePileStack.getNumber(),
				dismantlePileStack.getCreateTime(), dismantlePileStack.getCreatorId(),
				dismantlePileStack.getTerminalId(), dismantlePileStack.getWarehouseId(),
				dismantlePileStack.getProductId(), dismantlePileStack.getCurrentWrapCode(),
				dismantlePileStack.getSubWrapCodes(), dismantlePileStack.getOpreType(), dismantlePileStack.getRemarks(),
				dismantlePileStack.getId() 
				};
		final int[] argTypes = new int[] { Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.INTEGER, Types.INTEGER,
				Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.LONGVARCHAR, Types.SMALLINT, Types.VARCHAR,
				Types.INTEGER, Types.BIGINT };
		return jdbcTemplate.update(sql.toString(), params, argTypes);
	}


	@Override
	public List<DismantlePileStack> findListByCondition(final DismantlePileStack dismantlePileStack, final String enterpriseCode)
			throws DaoException {

		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(getTname(enterpriseCode, TableEnum.DISMANTLE_PILE_STACK));
		final List<Object> params = new ArrayList<Object>();
		try {
			if(dismantlePileStack!=null) {
				sql.append(" WHERE 1=1");
				if(StringUtils.isNotBlank(dismantlePileStack.getWorkOrder())) {
					sql.append(" AND _work_order = ?");
					params.add(dismantlePileStack.getWorkOrder());
				}
			}
			return jdbcTemplate.query(sql.toString(), params.toArray(), new DismantlePileStackRowMapper());
		} catch (DataAccessException e) {
			throw new DaoException(e);
		}
	}

	/**
	 * @ClassName: DismantlePileStackRowMapper
	 * @Description: 封装返回的实体
	 * @author youjun @date 2017-08-17 20:37:22
	 */
	public class DismantlePileStackRowMapper implements RowMapper<DismantlePileStack> {
		@Override
		public DismantlePileStack mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
			final DismantlePileStack dismantlePileStack = new DismantlePileStack();
			dismantlePileStack.setId(resultSet.getLong("_id"));
			dismantlePileStack.setWorkOrder(resultSet.getString("_work_order"));
			dismantlePileStack.setNumber(resultSet.getInt("_number"));
			dismantlePileStack.setCreateTime(resultSet.getTimestamp("_create_time"));
			dismantlePileStack.setCreatorId(resultSet.getInt("_creator_id"));
			dismantlePileStack.setTerminalId(resultSet.getInt("_terminal_id"));
			dismantlePileStack.setWarehouseId(resultSet.getInt("_warehouse_id"));
			dismantlePileStack.setProductId(resultSet.getInt("_product_id"));
			dismantlePileStack.setCurrentWrapCode(resultSet.getString("_current_wrap_code"));
			dismantlePileStack.setSubWrapCodes(resultSet.getString("_sub_wrap_codes"));
			dismantlePileStack.setOpreType(resultSet.getShort("_opre_type"));
			dismantlePileStack.setRemarks(resultSet.getString("_remarks"));
			return dismantlePileStack;
		}
	}

}
